import axios from 'axios'

var amsurl = localStorage.getItem('amsUrl');
var serverUrl = localStorage.getItem('serverUrl');

export const setUrl = () =>{
    serverUrl = localStorage.getItem('serverUrl');
    amsurl = localStorage.getItem('amsUrl');
}

/*test api*/
export const test = (para) =>{
    return axios.post(loginUrl+'/api/rooms/token',para,{headers:{
            'Content-Type':'application/json',
            'x-access-token':localStorage.getItem('zaccesstoken')
        }})
}

/*agent login*/
export const login = (name,pwd,org) =>{
  return axios.post(loginUrl+'/ivcs/api/v1/auth/logon',{
     userName:name,
     pwd:pwd,
     role:"agent",
     org:org
  },{
    "Content-Type":"application/json"
  })
}

export const genesyslogin = (name,pwd,place) =>{
    return axios.post(loginUrl+'/ivcs/api/v1/auth/logon',{
        userName:name,
        pwd:pwd,
        loginGenesys:true,
        place:place
    },{
        "Content-Type":"application/json"
    })
}


export const getsessiondetail = (sessionId) =>{
  return axios.get(loginUrl+'/ivcs/api/v1/sessions/'+sessionId+'/record',{headers:{
      'Content-Type':'application/json',
    }
  });
}

/*更新技能组*/
export const updateskill = (agentid,skill) =>{
    return axios.patch(amsurl+'/v1/agents/'+ agentid, [
      { "op": "replace", "path": "/skills", "value": [skill] }
    ],{
      "Content-Type":"application/json"
    })
}


export const createroom = (roomname) => {
  return axios.post(loginUrl+`/ivcs/api/v1/rooms`,{
    "name":roomname,
    "options":{}
  },{headers:{
      'Content-Type':'application/json',
      'x-access-token':localStorage.getItem('token')
    }})
}

export const getRoomToken = (roomid,userai) => {
  return axios.post(loginUrl+`/ivcs/api/v1/tokens`,{
    "room":roomid,
    "user":userai,
    "role":"presenter"

  },{headers:{
      'Content-Type':'application/json',
      'x-access-token':localStorage.getItem('token')
    }})
}


/*转接*/
export const transfer = (data) => {
  return axios.post(amsurl+`/v1/invitations`,data,{headers:{
      'Content-Type':'application/json',
    }})
}


/*获取访客提交的用户信息*/
export const getuserforminfo = (formid) =>{
  return axios.get(loginUrl+'/ivcs/api/v1/formInfo/'+formid,{headers:{
      'Content-Type':'application/json',
      'x-access-token':localStorage.getItem('token')
    }
  });
}

/*获取客户在线提交表单*/
export const getsessionformdata = (sessionId) =>{
  return axios.get(loginUrl+'/ivcs/api/v1/sessions/'+sessionId+'/applications',{headers:{
      'Content-Type':'application/json',
      'x-access-token':localStorage.getItem('token')
    }
  });
}

/*保存图片：（post）localhost:3001/api/uploadImg
{customerId, image}
*/
export const saveimg = (para) => {
  return axios.post(loginUrl+`/ivcs/api/v1//uploadImg`,para,{headers:{
      'Content-Type':'application/json',
      'x-access-token':localStorage.getItem('token')
    }})
}


export const startrecord = (url,data) =>{
  return axios.post(loginUrl+url,data,{headers:{
      'Content-Type':'application/json',
      'x-access-token':localStorage.getItem('token')
    }})
}

/*停止录制stream*/
export const stoprecording = (roomId,recordid) =>{
  return axios.delete(loginUrl+'/ivcs/api/v1/rooms/'+roomId+'/recordings/'+recordid,{headers:{
      'x-access-token':localStorage.getItem('token')
    }
  })
}

export const streamoutmystream = (roomid,para) => {
  return axios.post(`${loginUrl}`+'/ivcs/api/v1/rooms/'+roomid+'/streaming-outs',para,{headers:{
      'Content-Type':'application/json'
    }})
}

export const streaminrtmp = (roomid,para) => {
  return axios.post(`${loginUrl}`+'/ivcs/api/v1/rooms/'+roomid+'/streaming-ins',para,{headers:{
      'Content-Type':'application/json'
    }})
}

export const addmix = (roomid,streamId)=> {
  return axios.patch(loginUrl+'/ivcs/api/v1/rooms/'+roomid+'/streams/'+streamId,[{"op":"add","path":"/info/inViews","value":"common"}],{headers:{
      'Content-Type':'application/json',
      'x-access-token':localStorage.getItem('token')
    }})
}


/*保存session: (POST)localhost:3001/api/imcc/session
{customerId, sessionId}*/
export const savesession = (para) => {
  return axios.post(loginUrl+`/ivcs/api/v1/imcc/session`,para,{headers:{
      'Content-Type':'application/json',
      'x-access-token':localStorage.getItem('token')
    }})
}

export const savesessionform = (para) => {
  return axios.post(loginUrl+`/ivcs/api/v1/session/form`,para,{headers:{
      'Content-Type':'application/json',
      'x-access-token':localStorage.getItem('token')
    }})
}

export const getVideoLinkResource = ()=>{
    return axios.get(loginUrl+'/ivcs/api/v1/public/resource-link',{
        headers:{
            'Content-Type':'application/json'
        }});
}

/**
 * 获取所有通话中数据
 * @param page  当前页
 * @param pageSize 每页多少条数据
 * @param startCallTime 接通时间
 * @param startDialingTime 通话时长
 * @param agentId 座席 name
 * @param groupId 组 id
 * @param isRobot 是否机器人
 * @param sessionId 会话 id
 */

export const getSessionsList = (data) => {
  console.log(data);
  return axios.get(
    loginUrl + `/ivcs/api/v1/sessions/list-ongoing?page=${data.page}&pageSize=${data.pageSize}&org=${guowangOrg}&startCallTime=${data.startCallTime}&startDialingTime=${data.startDialingTime}&agentId=${data.agentId}&groupId=${data.groupId}&isRobot=${data.isRobot}&sessionId=${data.sessionId}`, { headers: {
         'Content-Type':'application/json',
         'x-access-token':localStorage.getItem('token')
     }
 })
}


export const storageLoginData = (result,resp) =>{
    localStorage.setItem('name',result.data.userName);
    localStorage.setItem('token',resp.data.data.accessToken);
    localStorage.setItem('userId',result.data.id);
    localStorage.setItem('org',result.data.org);
    var urls = resp.data.data.urls;
    localStorage.setItem('loginUrl',urls.backendurl);
    localStorage.setItem('amsUrl',urls.amsurl);
    localStorage.setItem('uploadUrl',urls.backendurl+'/ivcs/api/v1/upload');
    localStorage.setItem('turnloginUrl',urls.turnloginUrl);
    localStorage.setItem('turnserverCredential',urls.turnserverCredential);
    localStorage.setItem('turnserverUsername',urls.turnserverUsername);
    localStorage.setItem('rtmpServer',urls.rtmpServer);
}
